package com.android.View;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.android.GernerateKeySet.CommunicateNFC;
import com.android.GernerateKeySet.PreferenceInfo;


public class StartMenuActivity extends Activity {
	

	private SharedPreferences sharedPref;
	@Override	
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		String action = intent.getAction();
		

		/** Preference **/
		sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
		PreferenceInfo preInfo = new PreferenceInfo(sharedPref);

		
		
		
		if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
			setContentView(R.layout.loginpage);
			
			ProgressDialog dialog = new ProgressDialog(StartMenuActivity.this);
			dialog.setCancelable(true);
			dialog.setMessage("NFC Connecting...");
			dialog.show();
			
			CommunicateNFC nfcCommunicate = new CommunicateNFC();
			if (nfcCommunicate.communicate(getIntent(),preInfo)){
				dialog.cancel();
				System.out.println("nfc return true");
				Toast.makeText(this, "인증서 전송 성공", Toast.LENGTH_SHORT).show();
				finish();
			}else{
				// shows error msg
				System.out.println("nfc return false");
			}
			
		}else{
			setContentView(R.layout.main);
			Button register = (Button) findViewById(R.id.registerbtn);
			Button login = (Button) findViewById(R.id.loginbtn);

			register.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(StartMenuActivity.this, InputUserInfoActivity.class);
					startActivity(intent);
				}
			});
			
			login.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(StartMenuActivity.this, CertSelectActivity.class);
//					intent.putExtra("certName", preInfo.getCertfile());
//					intent.putExtra("certName", preInfo.getPVfile());
					startActivity(intent);
				}
			});
		}
		
	}


	
}
